//==============================================================================
//
//               Copyright (C) 2005  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2005-05-06 05:53:34 $ $Revision: 1.1 $
//
//==============================================================================

#include <stdlib.h>
#include <math.h>

#include "ofc/DDiscreteDistribution.h"

#include "DInc.h"
#include "DTest.h"


//-Datatype--------------------------------------------------------------------

void DDiscreteDistribution_test(void)
{
  DDiscreteDistribution *dis   = [DDiscreteDistribution new];
  DListIterator         *iter  = nil;
  DScore                *score = nil;

  double d;
  
  STARTTEST();

  TEST([dis length] == 0);
  
  for (d = 0.0; d < 5.0; d += 0.5)
  {
    [dis range :d :d+0.5];
  }

  TEST(![dis update :-0.1]);
  TEST( [dis update :0.1 ]);
  TEST( [dis update :4.9 ]);
  TEST( [dis update :0.0 ]);
  TEST( [dis update :2.1 ]);
  TEST( [dis update :3.7 ]);
  TEST( [dis update :3.6 ]);
  TEST( [dis update :3.5 ]);
  TEST( [dis update :3.4 ]);
  TEST( [dis update :4.6 ]);
  TEST( [dis update :4.7 ]);
  TEST(![dis update :5.0 ]);
  
  TEST([dis length] == 10);
  TEST(fabs([dis sum] - 30.6) < 0.1);
  TEST(fabs([dis sumSquared] - 122.14) < 0.01);
  TEST(fabs([dis mean] - 3.06) < 0.01);
  TEST(fabs([dis standardDeviation] - 1.6883) < 0.0001);

  iter = [dis scores];
  
  score = [iter first]; TEST(score != nil); TEST([score length] == 2); TEST(fabs([score percentage] - 20.0) < 0.1);
  
  score = [iter next]; TEST(score != nil); TEST([score length] == 0); TEST(fabs([score percentage] - 0.0) < 0.1);
  score = [iter next]; TEST(score != nil); TEST([score length] == 0); TEST(fabs([score percentage] - 0.0) < 0.1);
  score = [iter next]; TEST(score != nil); TEST([score length] == 0); TEST(fabs([score percentage] - 0.0) < 0.1);
  score = [iter next]; TEST(score != nil); TEST([score length] == 1); TEST(fabs([score percentage] - 10.0) < 0.1);
  score = [iter next]; TEST(score != nil); TEST([score length] == 0); TEST(fabs([score percentage] - 0.0) < 0.1);
  score = [iter next]; TEST(score != nil); TEST([score length] == 1); TEST(fabs([score percentage] - 10.0) < 0.1);
  score = [iter next]; TEST(score != nil); TEST([score length] == 3); TEST(fabs([score percentage] - 30.0) < 0.1);
  score = [iter next]; TEST(score != nil); TEST([score length] == 0); TEST(fabs([score percentage] - 0.0) < 0.1);
  score = [iter next]; TEST(score != nil); TEST([score length] == 3); TEST(fabs([score percentage] - 30.0) < 0.1);
  score = [iter next]; TEST(score == nil); TEST([score length] == 0); 

  [dis reset];
  
  TEST([dis length] == 0);
  TEST([dis sum] == 0.0);
  TEST([dis sumSquared] == 0.0);
  TEST([dis mean] == 0.0);
  TEST([dis standardDeviation] == 0.0);
  
  score = [iter first];
  
  TEST(score != nil); TEST([score length] == 0); TEST([score percentage] == 100.0); TEST([score max] == 0.5);
  
  score = [iter last];

  TEST(score != nil); TEST([score length] == 0); TEST([score percentage] == 100.0); TEST([score min] == 4.5);
  
  score = nil;
  
  [iter free];  
  [dis free];
  
  STOPTEST();
}

